package com.mobogenie.blur;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class DownloadManagerRecommendedAdapter extends BaseAdapter {

    public static final String TAG = "DownloadManagerRecommendedAdapter";

    public static final String SAMPLE_PATH = "samples";

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    List<AppInfo> mSamplelist = new ArrayList<AppInfo>(3);

    public DownloadManagerRecommendedAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);

        updateAllSamples();
    }

    public static class AppInfo {
        public String title;
        public long size;
    }

    private void updateAllSamples() {
        mSamplelist.clear();
        try {
            String[] samples = mContext.getAssets().list(SAMPLE_PATH);
            for (String sample : samples) {
                AppInfo info = new AppInfo();
                info.title = sample;
                InputStream inputStream = mContext.getAssets().open(SAMPLE_PATH + "/" + sample);
                info.size = inputStream.available();
                inputStream.close();
                mSamplelist.add(info);
            }
        } catch (Exception e) {
            // Ignore.
        }
    }

    public List<AppInfo> getAllSamples() {
        return mSamplelist;
    }

    @Override
    public int getCount() {
        return mSamplelist.size();
    }

    @Override
    public Object getItem(int position) {
        return mSamplelist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.download_recommended_app_item, parent, false);
            holder = new ViewHolder();
            holder.iconView = (ImageView) convertView.findViewById(R.id.app_icon);
            holder.nameView = (TextView) convertView.findViewById(R.id.app_name);
            holder.sizeView = (TextView) convertView.findViewById(R.id.app_size);
            holder.appInstallView = convertView.findViewById(R.id.app_install);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
//        AppInfo info = mSamplelist.get(position);
//        holder.iconView.setImageResource(R.drawable.icon_l);
//        holder.nameView.setText(info.title);
//        holder.sizeView.setText(Formatter.formatFileSize(mContext, info.size));
//        holder.appInstallView.setVisibility(View.GONE);
        return convertView;
    }

    private static class ViewHolder {
        ImageView iconView;
        TextView nameView;
        TextView sizeView;
        View appInstallView;
    }
}
